﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Common
{
    [Serializable]
    public class 换热站Data
    {

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string 名称Field = "@名称";

        public const string 地址Field = "@地址";

        public const string 备注Field = "@备注";

        public const string ISAVAILABLEField = "@IsAvailable";

        public const string 锅炉房IDField = "@锅炉房ID";

        public const string OPERATORField = "@Operator";

        public const string ADDTIMEField = "@AddTime";

        private Guid _ID;

        private String _名称;

        private String _地址;

        private String _备注;

        private Boolean _IsAvailable;

        private Guid _锅炉房ID;

        private String _Operator;

        private DateTime _AddTime;

        public Guid ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String 名称
        {
            get { return _名称; }
            set { _名称 = value; }
        }
        public String 地址
        {
            get { return _地址; }
            set { _地址 = value; }
        }
        public String 备注
        {
            get { return _备注; }
            set { _备注 = value; }
        }
        public Boolean IsAvailable
        {
            get { return _IsAvailable; }
            set { _IsAvailable = value; }
        }
        public Guid 锅炉房ID
        {
            get { return _锅炉房ID; }
            set { _锅炉房ID = value; }
        }
        public String Operator
        {
            get { return _Operator; }
            set { _Operator = value; }
        }
        public DateTime AddTime
        {
            get { return _AddTime; }
            set { _AddTime = value; }
        }
        public 换热站Data()
        {
            _ID = Guid.Empty;
            _名称 = "";
            _地址 = "";
            _备注 = "";
            _IsAvailable = false;
            _锅炉房ID = Guid.Empty;
            _Operator = "";
            _AddTime = DateTime.Now;
            SQLType = 0;
        }
        public int SQLType;
    }

    public class 换热站DataFactory
    {
        public static 换热站Data Construct(IDataReader reader)
        {
            换热站Data data = new 换热站Data();

            data.ID = (Guid)(reader["ID"]);

            data.名称 = Convert.ToString(reader["名称"]);

            data.地址 = Convert.ToString(reader["地址"]);

            data.备注 = Convert.ToString(reader["备注"]);

            data.IsAvailable = Convert.ToBoolean(reader["IsAvailable"]);

            data.锅炉房ID = (Guid)(reader["锅炉房ID"]);

            data.Operator = Convert.ToString(reader["Operator"]);

            data.AddTime = Convert.ToDateTime(reader["AddTime"]);

            return data;
        }
        public static Guid GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dt.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Guid.Empty;

            try { PrimaryKey = (Guid)(dr[换热站Data.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static 换热站Data Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static 换热站Data Construct(DataTable dt, int rowIndex)
        {
            换热站Data data = new 换热站Data();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.名称 = Convert.ToString(dr["名称"]); }
            catch { }

            try { data.地址 = Convert.ToString(dr["地址"]); }
            catch { }

            try { data.备注 = Convert.ToString(dr["备注"]); }
            catch { }

            try { data.IsAvailable = Convert.ToBoolean(dr["IsAvailable"]); }
            catch { }

            try { data.锅炉房ID = new Guid(dr["锅炉房ID"].ToString()); }
            catch { }

            try { data.Operator = Convert.ToString(dr["Operator"]); }
            catch { }

            try { data.AddTime = Convert.ToDateTime(dr["AddTime"]); }
            catch { }

            return data;
        }
        public static 换热站Data Construct(DataGridView dgv, int rowIndex)
        {
            换热站Data data = new 换热站Data();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.名称 = Convert.ToString(dr["名称"]); }
            catch { }

            try { data.地址 = Convert.ToString(dr["地址"]); }
            catch { }

            try { data.备注 = Convert.ToString(dr["备注"]); }
            catch { }

            try { data.IsAvailable = Convert.ToBoolean(dr["IsAvailable"]); }
            catch { }

            try { data.锅炉房ID = new Guid(dr["锅炉房ID"].ToString()); }
            catch { }

            try { data.Operator = Convert.ToString(dr["Operator"]); }
            catch { }

            try { data.AddTime = Convert.ToDateTime(dr["AddTime"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dgv.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return Guid.Empty;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == 换热站Data.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = (Guid)(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
    }
}